from flask import Flask
import config
from exts import db, mail, cache, csrf, avatars, jwt, cors
from flask_migrate import Migrate
from models import auth, common
from apps.front import bp_front
from apps.cmsapi import bp_csm
from bbs_celery import make_celery
from apps.media import bp_media
import commands

app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
mail.init_app(app)
migrate = Migrate(app, db)
mycelery = make_celery(app)
cache.init_app(app)
csrf.init_app(app)
avatars.init_app(app)
jwt.init_app(app)
cors.init_app(app, resources={r"/cmsapi/*": {"origins": "*"}})

# 排除cmsapi的csrf验证
csrf.exempt(bp_csm)

# 注册蓝图
app.register_blueprint(bp_front)
app.register_blueprint(bp_media)
app.register_blueprint(bp_csm)

# 注册命令
app.cli.command("init_board")(commands.init_board)
app.cli.command("init_role")(commands.init_role)
# app.cli.command("create_test_posts")(commands.create_test_posts)
# app.cli.command("bind_roles")(commands.bind_roles)
app.cli.command("init_user")(commands.init_user)

# 将ORM模型映射到数据库0
# 1. migrate=Migrate(app, db)
# 2. 初始化迁移仓库 flask db init
# 3. 将ORM模型生成迁移脚本 flask db migrate
# 4. 运行迁移脚本生成表 flask db upgrade


if __name__ == '__main__':
    app.run(host="0.0.0.0")
